<?php
class RecruitmentsController extends AppController {

	var $name = 'Recruitments';
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('index', 'view', 'category', 'ncth_cat', 'ncth_home', 'home', 'iframe');
	}
	
	function iframe() {		
		$this->Recruitment->recursive = 0;
		$this->layout = 'iframe';
		
		$conditions = array();
		
		
		$cities = $this->Recruitment->City->find('all');
		
		$types = $this->Recruitment->RecruitmentType->find('all');
		foreach($types as $t => $item)
		{
			$types[$t]['products'] = $this->Recruitment->find('all', array('conditions'=>array('Recruitment.recruitment_type_id'=>$item['RecruitmentType']['id']), 'limit'=>10, 'order'=>'create_date DESC'));
			foreach($types[$t]['products'] as $k => $item)
			{
				//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
				//$Recruitments_vip[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
				//$types[$t]['products'][$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"]);
				//$Recruitments_vip[$k]["Recruitment"]["description"] = parent::snippet($item["Recruitment"]["description"], 400);
				//$Recruitments_vip[$k]["Recruitment"]["image"] = parent::getImage($item["Recruitment"]["description"]);
				
			}
			
		}
		
		$Recruitments_ncth = $this->Recruitment->find('all', array('conditions'=>array('Recruitment.Recruitment_type_id'=>array(2,3)), 'order'=>'Recruitment.create_date DESC'));
		foreach($Recruitments_ncth as $k => $item)
		{
			//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
			$Recruitments_ncth[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
			//$Recruitments_vip[$k]["Recruitment"]["name"] = parent::snippet($item["Recruitment"]["name"], 40);
			//$Recruitments_vip[$k]["Recruitment"]["description"] = parent::snippet($item["Recruitment"]["description"], 400);
			$Recruitments_ncth[$k]["Recruitment"]["image"] = $item["RecruitmentImage"]["filename"];
			$Recruitments_ncth[$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"]);
			
		}
		
		//$this->set('Recruitments', $Recruitments);
		$this->set('types', $types);
		$this->set('Recruitments_ncth', $Recruitments_ncth);
		$this->set('cities', $cities);
		//$this->set('city_id', $city_id);
		//$this->set('districts', $districts);		
		//$this->set('district_id', $district_id);
		//$this->set('careers', $careers);		
		//$this->set('career_id', $career_id);
		//$this->set('keyword', $keyword);	
	}

	function index() {		
		$this->Recruitment->recursive = 0;
		$this->layout = 'home';
		
		$conditions = array();
		
		
		$cities = $this->Recruitment->City->find('all');
		
		$types = $this->Recruitment->RecruitmentType->find('all');
		foreach($types as $t => $item)
		{
			$types[$t]['products'] = $this->Recruitment->find('all', array('conditions'=>array('Recruitment.recruitment_type_id'=>$item['RecruitmentType']['id']), 'limit'=>10, 'order'=>'create_date DESC'));
			foreach($types[$t]['products'] as $k => $item)
			{
				//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
				//$Recruitments_vip[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
				//$types[$t]['products'][$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"]);
				//$Recruitments_vip[$k]["Recruitment"]["description"] = parent::snippet($item["Recruitment"]["description"], 400);
				//$Recruitments_vip[$k]["Recruitment"]["image"] = parent::getImage($item["Recruitment"]["description"]);
				
			}
			
		}
		
		$Recruitments_ncth = $this->Recruitment->find('all', array('conditions'=>array('Recruitment.Recruitment_type_id'=>array(2,3)), 'order'=>'Recruitment.create_date DESC'));
		foreach($Recruitments_ncth as $k => $item)
		{
			//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
			$Recruitments_ncth[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
			//$Recruitments_vip[$k]["Recruitment"]["name"] = parent::snippet($item["Recruitment"]["name"], 40);
			//$Recruitments_vip[$k]["Recruitment"]["description"] = parent::snippet($item["Recruitment"]["description"], 400);
			$Recruitments_ncth[$k]["Recruitment"]["image"] = $item["RecruitmentImage"]["filename"];
			$Recruitments_ncth[$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"]);
			
		}
		
		//$this->set('Recruitments', $Recruitments);
		$this->set('types', $types);
		$this->set('Recruitments_ncth', $Recruitments_ncth);
		$this->set('cities', $cities);
		//$this->set('city_id', $city_id);
		//$this->set('districts', $districts);		
		//$this->set('district_id', $district_id);
		//$this->set('careers', $careers);		
		//$this->set('career_id', $career_id);
		//$this->set('keyword', $keyword);	
	}
	
	function home() {		
		$this->Recruitment->recursive = 0;
		$this->layout = 'home';
		Controller::loadModel('Apply');
		Controller::loadModel('ApplyType');
		
		$conditions = array();
		
		
		$cities = $this->Recruitment->City->find('all');
		
		$types = $this->Recruitment->RecruitmentType->find('all');
		foreach($types as $t => $item)
		{
			$types[$t]['products'] = $this->Recruitment->find('all', array('conditions'=>array('Recruitment.recruitment_type_id'=>$item['RecruitmentType']['id']), 'limit'=>10, 'order'=>'create_date DESC'));
			foreach($types[$t]['products'] as $k => $item)
			{
				//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
				//$Recruitments_vip[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
				//$types[$t]['products'][$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"]);
				//$Recruitments_vip[$k]["Recruitment"]["description"] = parent::snippet($item["Recruitment"]["description"], 400);
				//$Recruitments_vip[$k]["Recruitment"]["image"] = parent::getImage($item["Recruitment"]["description"]);
				
			}
			
		}
		
		$types_2 = $this->ApplyType->find('all');
		foreach($types_2 as $t => $item)
		{
			$types_2[$t]['products'] = $this->Apply->find('all', array('conditions'=>array('Apply.apply_type_id'=>$item['ApplyType']['id']), 'limit'=>10, 'order'=>'create_date DESC'));
			foreach($types_2[$t]['products'] as $k => $item)
			{
				//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
				//$Recruitments_vip[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
				//$types[$t]['products'][$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"]);
				//$Recruitments_vip[$k]["Recruitment"]["description"] = parent::snippet($item["Recruitment"]["description"], 400);
				//$Recruitments_vip[$k]["Recruitment"]["image"] = parent::getImage($item["Recruitment"]["description"]);
				
			}
			
		}
		
		
		
		//$this->set('Recruitments', $Recruitments);
		$this->set('types', $types);
		$this->set('types_2', $types_2);
		$this->set('cities', $cities);
		//$this->set('city_id', $city_id);
		//$this->set('districts', $districts);		
		//$this->set('district_id', $district_id);
		//$this->set('careers', $careers);		
		//$this->set('career_id', $career_id);
		//$this->set('keyword', $keyword);	
	}
	
	function ncth() {		
		$this->Recruitment->recursive = 0;
		$this->layout = 'home';
		
		$conditions = array();
		
		////filter city id
		$cities = $this->Recruitment->City->find('all');		
		
		$types = $this->Recruitment->RecruitmentType->find('all');
		foreach($types as $t => $item)
		{
			$types[$t]['products'] = $this->Recruitment->find('all', array('conditions'=>array('Recruitment.Recruitment_type_id'=>$item['RecruitmentType']['id']), 'limit'=>10, 'order'=>'create_date DESC'));
			foreach($types[$t]['products'] as $k => $item)
			{
				//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
				//$Recruitments_vip[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
				$types[$t]['products'][$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"]);
				//$Recruitments_vip[$k]["Recruitment"]["description"] = parent::snippet($item["Recruitment"]["description"], 400);
				//$Recruitments_vip[$k]["Recruitment"]["image"] = parent::getImage($item["Recruitment"]["description"]);
				
			}
			
		}
		
		$Recruitments_vip = $this->Recruitment->find('all', array('order'=>'Recruitment.hit DESC', 'limit'=>5));
		foreach($Recruitments_vip as $k => $item)
		{
			//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
			$Recruitments_vip[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
			//$Recruitments_vip[$k]["Recruitment"]["name"] = parent::snippet($item["Recruitment"]["name"], 40);
			//$Recruitments_vip[$k]["Recruitment"]["description"] = parent::snippet($item["Recruitment"]["description"], 400);
			$Recruitments_vip[$k]["Recruitment"]["image"] = $item["RecruitmentImage"]["filename"];
			$Recruitments_vip[$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"]);
			
		}
		
		//$this->set('Recruitments', $Recruitments);
		$this->set('types', $types);
		$this->set('Recruitments_vip', $Recruitments_vip);
		$this->set('cities', $cities);
		//$this->set('city_id', $city_id);
		//$this->set('districts', $districts);		
		//$this->set('district_id', $district_id);
		//$this->set('careers', $careers);		
		//$this->set('career_id', $career_id);
		//$this->set('keyword', $keyword);	
	}
	
	function category($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Recruitment Cat', true));
			$this->redirect(array('action' => 'index'));
		}
		
		$type = $this->Recruitment->RecruitmentType->read(null, $id);
		
		$this->Recruitment->recursive = 0;
		$this->layout = 'home';
		
		$conditions = array('Recruitment.Recruitment_type_id'=>$id);
		
		//filter city id
		$cities = $this->Recruitment->City->find('all');
		//$city_id = $this->Session->read("districts_city_id") ? $this->Session->read("districts_city_id"): "";
		if(!empty($this->params['form']['filter_city_id']))
			$city_id = $this->params['form']['filter_city_id'];
		else
			$city_id = $this->Session->read("Recruitment_city_id");
		
		$this->Session->write("Recruitment_city_id", $city_id);
		
		if($city_id && $city_id != "all")
		{
			$conditions["Recruitment.city_id"] = $city_id;			
		}
		
		//echo $this->Session->read("districts_city_id");
		
		//filter district id
		$districts = $this->Recruitment->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		if(!empty($this->params['form']['filter_district_id']))
			$district_id = $this->params['form']['filter_district_id'];
		else
			$district_id = $this->Session->read("Recruitment_district_id");
		
		$this->Session->write("Recruitment_district_id", $district_id);
		
		if($district_id && $district_id != "all")
		{
			$conditions["Recruitment.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Recruitment->Career->find('all');		
		if(!empty($this->params['form']['filter_career_id']))
			$career_id = $this->params['form']['filter_career_id'];
		else
			$career_id = $this->Session->read("Recruitment_career_id");
		
		$this->Session->write("Recruitment_career_id", $career_id);
		
		if($career_id && $career_id != "all")
		{
			$conditions["Recruitment.career_id"] = $career_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter keyword
		$keyword = $this->Session->read("Recruitment_keyword");
		if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] != '')
		{
			$keyword = $this->params['form']['filter_keyword'];
			$this->Session->write("Recruitment_keyword", $keyword);
		}
		if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] == '')
		{
			$keyword = '';
			$this->Session->write("Recruitment_keyword", $keyword);
		}
		
		
		
		if($keyword && $keyword != "")
		{
			//$conditions["Recruitment.name LIKE ?"] = '%'.$keyword.'%';
			//$conditions["Product.name LIKE ?"] = '%'.$keyword.'%';
			$conditions["OR"] = array("Recruitment.o_name LIKE ?"=>'%'.$keyword.'%', "Recruitment.fullname LIKE ?"=>'%'.$keyword.'%');
		}
		
		//var_dump($this->params);
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions, 'order'=>'create_date DESC'				
			);
		}
		
		$Recruitments = $this->paginate();
		foreach($Recruitments as $k => $item)
		{
			//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
			$Recruitments[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
			//$Recruitments[$k]["Recruitment"]["name"] = parent::snippet($item["Recruitment"]["name"], 40);
			//$Recruitments[$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"], 40);
		}
		
		
		
		$this->set('Recruitments', $Recruitments);		
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);		
		$this->set('career_id', $career_id);
		$this->set('keyword', $keyword);
		$this->set('type', $type);
		$this->set('category_id', $id);
	}
	
	function ncth_cat() {
				
		//$type = $this->Recruitment->RecruitmentType->read(null, $id);
		
		$this->Recruitment->recursive = 0;
		$this->layout = 'home';
		
		$conditions = array('Recruitment.Recruitment_type_id'=>array(2,3));
		
		//filter city id
		$cities = $this->Recruitment->City->find('all');
		//$city_id = $this->Session->read("districts_city_id") ? $this->Session->read("districts_city_id"): "";
		if(!empty($this->params['form']['filter_city_id']))
			$city_id = $this->params['form']['filter_city_id'];
		else
			$city_id = $this->Session->read("Recruitment_city_id");
		
		$this->Session->write("Recruitment_city_id", $city_id);
		
		if($city_id && $city_id != "all")
		{
			$conditions["Recruitment.city_id"] = $city_id;			
		}
		
		//echo $this->Session->read("districts_city_id");
		
		//filter district id
		$districts = $this->Recruitment->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		if(!empty($this->params['form']['filter_district_id']))
			$district_id = $this->params['form']['filter_district_id'];
		else
			$district_id = $this->Session->read("Recruitment_district_id");
		
		$this->Session->write("Recruitment_district_id", $district_id);
		
		if($district_id && $district_id != "all")
		{
			$conditions["Recruitment.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Recruitment->Career->find('all');		
		if(!empty($this->params['form']['filter_career_id']))
			$career_id = $this->params['form']['filter_career_id'];
		else
			$career_id = $this->Session->read("Recruitment_career_id");
		
		$this->Session->write("Recruitment_career_id", $career_id);
		
		if($career_id && $career_id != "all")
		{
			$conditions["Recruitment.career_id"] = $career_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter keyword
		$keyword = $this->Session->read("Recruitment_keyword");
		if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] != '')
		{
			$keyword = $this->params['form']['filter_keyword'];
			$this->Session->write("Recruitment_keyword", $keyword);
		}
		if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] == '')
		{
			$keyword = '';
			$this->Session->write("Recruitment_keyword", $keyword);
		}
		
		
		
		if($keyword && $keyword != "")
		{
			//$conditions["Recruitment.name LIKE ?"] = '%'.$keyword.'%';
			//$conditions["Product.name LIKE ?"] = '%'.$keyword.'%';
			$conditions["OR"] = array("Recruitment.o_name LIKE ?"=>'%'.$keyword.'%', "Recruitment.fullname LIKE ?"=>'%'.$keyword.'%');
		}
		
		//var_dump($this->params);
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions, 'order'=>'create_date DESC'				
			);
		}
		
		$Recruitments = $this->paginate();
		foreach($Recruitments as $k => $item)
		{
			//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
			$Recruitments[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
			//$Recruitments[$k]["Recruitment"]["name"] = parent::snippet($item["Recruitment"]["name"], 40);
			$Recruitments[$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"], 40);
		}
		
		$career = $this->Recruitment->Career->read(null, $career_id);
		
		$this->set('Recruitments', $Recruitments);		
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);		
		$this->set('career_id', $career_id);
		$this->set('keyword', $keyword);
		$this->set('career', $career);
		//$this->set('category_id', $id);
	}
	
	function ncth_home() {
				
		
		$careersx = $this->Recruitment->Career->find('all');
		
		foreach($careersx as $kk => $itemk)
		{
			$careersx[$kk]['products'] = $this->Recruitment->find('all', array('conditions'=>array('Recruitment.career_id'=>$itemk['Career']['id'], 'Recruitment.Recruitment_type_id'=>array(2,3)),'order'=>'Recruitment.create_date DESC'));
			if(!count($careersx[$kk]['products']))
			{
				unset($careersx[$kk]);
			}			
		}
		
		
		$this->Recruitment->recursive = 0;
		$this->layout = 'home';
		
		$conditions = array('Recruitment.Recruitment_type_id'=>array(2,3));
		
		//filter city id
		$cities = $this->Recruitment->City->find('all');
		//$city_id = $this->Session->read("districts_city_id") ? $this->Session->read("districts_city_id"): "";
		if(!empty($this->params['form']['filter_city_id']))
			$city_id = $this->params['form']['filter_city_id'];
		else
			$city_id = $this->Session->read("Recruitment_city_id");
		
		$this->Session->write("Recruitment_city_id", $city_id);
		
		if($city_id && $city_id != "all")
		{
			$conditions["Recruitment.city_id"] = $city_id;			
		}
		
		//echo $this->Session->read("districts_city_id");
		
		//filter district id
		$districts = $this->Recruitment->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		if(!empty($this->params['form']['filter_district_id']))
			$district_id = $this->params['form']['filter_district_id'];
		else
			$district_id = $this->Session->read("Recruitment_district_id");
		
		$this->Session->write("Recruitment_district_id", $district_id);
		
		if($district_id && $district_id != "all")
		{
			$conditions["Recruitment.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Recruitment->Career->find('all');		
		if(!empty($this->params['form']['filter_career_id']))
			$career_id = $this->params['form']['filter_career_id'];
		else
			$career_id = $this->Session->read("Recruitment_career_id");
		
		$this->Session->write("Recruitment_career_id", $career_id);
		
		if($career_id && $career_id != "all")
		{
			$conditions["Recruitment.career_id"] = $career_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter keyword
		$keyword = $this->Session->read("Recruitment_keyword");
		if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] != '')
		{
			$keyword = $this->params['form']['filter_keyword'];
			$this->Session->write("Recruitment_keyword", $keyword);
		}
		if(isset($this->params['form']['filter_keyword']) && $this->params['form']['filter_keyword'] == '')
		{
			$keyword = '';
			$this->Session->write("Recruitment_keyword", $keyword);
		}
		
		
		
		if($keyword && $keyword != "")
		{
			//$conditions["Recruitment.name LIKE ?"] = '%'.$keyword.'%';
			//$conditions["Product.name LIKE ?"] = '%'.$keyword.'%';
			$conditions["OR"] = array("Recruitment.o_name LIKE ?"=>'%'.$keyword.'%', "Recruitment.fullname LIKE ?"=>'%'.$keyword.'%');
		}
		
		//var_dump($this->params);
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions, 'order'=>'create_date DESC'				
			);
		}
		
		$Recruitments = $this->paginate();
		foreach($Recruitments as $k => $item)
		{
			//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
			$Recruitments[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
			//$Recruitments[$k]["Recruitment"]["name"] = parent::snippet($item["Recruitment"]["name"], 40);
			$Recruitments[$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"], 40);
		}
		
		$career = $this->Recruitment->Career->read(null, $career_id);
		
		$this->set('Recruitments', $Recruitments);		
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);
		$this->set('careersx', $careersx);
		$this->set('career_id', $career_id);
		$this->set('keyword', $keyword);
		$this->set('career', $career);
		//$this->set('category_id', $id);
	}
	
	function view($id = null) {
		$this->layout = 'home';
		if (!$id) {
			$this->Session->setFlash(__('Invalid Recruitment', true));
			$this->redirect(array('action' => 'index'));
		}
		$Recruitment = $this->Recruitment->read(null, $id);
		$Recruitment["Recruitment"]["hit"] += 1;
		$this->Recruitment->save($Recruitment);			
		
		//$Recruitment["Recruitment"]["price"] = $Recruitment["Recruitment"]["price"] != '' ? number_format($Recruitment["Recruitment"]["price"],0,",", "."): '';
		$this->set('Recruitment', $Recruitment);
		
		
		$this->Recruitment->recursive = 0;
		
		$conditions = array('Recruitment.Recruitment_type_id'=>$Recruitment["RecruitmentType"]["id"]);
		
		//filter city id
		$cities = $this->Recruitment->City->find('all');
		$city_id = $Recruitment["Recruitment"]["city_id"];		
		
		if($city_id && $city_id != "all")
		{
			$conditions["Recruitment.city_id"] = $city_id;			
		}
		
		//filter district id
		$districts = $this->Recruitment->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		$district_id = $Recruitment["Recruitment"]["district_id"];
		
		if($district_id && $district_id != "all")
		{
			$conditions["Recruitment.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Recruitment->Career->find('all');		
		$career_id = $Recruitment["Recruitment"]["career_id"];
		
		if($career_id && $career_id != "all")
		{
			$conditions["Recruitment.career_id"] = $career_id;			
		}		

		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions				
			);
		}
		
		$Recruitments = $this->paginate();
		
		foreach($Recruitments as $k => $item)
		{
			//$Recruitments[$k]["Recruitment"]["price"] = $item["Recruitment"]["price"] != '' ? number_format($item["Recruitment"]["price"],0,",", "."): '';
			$Recruitments[$k]["Recruitment"]["create_date"] = date('d/m/Y', strtotime($item["Recruitment"]["create_date"]));
			//$Recruitments[$k]["Recruitment"]["name"] = parent::snippet($item["Recruitment"]["name"], 40);
			//$Recruitments[$k]["Recruitment"]["name"] = $item["Recruitment"]["o_name"] != 'none' ? parent::snippet($item["Recruitment"]["o_name"], 40) : parent::snippet($item["Recruitment"]["fullname"], 40);
		}
		
		$this->set('Recruitments', $Recruitments);
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);		
		$this->set('career_id', $career_id);
		$this->set('keyword', '');
		$this->set('category_id', $Recruitment["RecruitmentType"]["id"]);
	}

	function admin_index() {
		$this->Recruitment->recursive = 0;
		
		$conditions = array();
		
		//filter city id
		$cities = $this->Recruitment->City->find('all');
		//$city_id = $this->Session->read("districts_city_id") ? $this->Session->read("districts_city_id"): "";
		if(!empty($this->params['form']['filter_city_id']))
			$city_id = $this->params['form']['filter_city_id'];
		else
			$city_id = $this->Session->read("Recruitment_city_id");
		
		$this->Session->write("Recruitment_city_id", $city_id);
		
		if($city_id && $city_id != "all")
		{
			$conditions["Recruitment.city_id"] = $city_id;			
		}
		
		//filter type
		$types = $this->Recruitment->RecruitmentType->find('all');
		//$city_id = $this->Session->read("districts_city_id") ? $this->Session->read("districts_city_id"): "";
		if(!empty($this->params['form']['filter_type_id']))
			$type_id = $this->params['form']['filter_type_id'];
		else
			$type_id = $this->Session->read("Recruitment_type_id");
		
		$this->Session->write("Recruitment_type_id", $type_id);
		
		if($type_id && $type_id != "all")
		{
			$conditions["Recruitment.Recruitment_type_id"] = $type_id;			
		}
		
		//echo $this->Session->read("districts_city_id");
		
		//filter district id
		$districts = $this->Recruitment->District->find('all', array("conditions"=>array("District.city_id"=>$city_id)));		
		if(!empty($this->params['form']['filter_district_id']))
			$district_id = $this->params['form']['filter_district_id'];
		else
			$district_id = $this->Session->read("Recruitment_district_id");
		
		$this->Session->write("Recruitment_district_id", $district_id);
		
		if($district_id && $district_id != "all")
		{
			$conditions["Recruitment.district_id"] = $district_id;			
		}
		//echo $this->Session->read("districts_city_id");
		
		//filter career id
		$careers = $this->Recruitment->Career->find('all');		
		if(!empty($this->params['form']['filter_career_id']))
			$career_id = $this->params['form']['filter_career_id'];
		else
			$career_id = $this->Session->read("Recruitment_career_id");
		
		$this->Session->write("Recruitment_career_id", $career_id);
		
		if($career_id && $career_id != "all")
		{
			$conditions["Recruitment.career_id"] = $career_id;			
		}
		
		////filter keyword		
		//if(!empty($this->params['form']['filter_keyword']))
		//	$keyword = $this->params['form']['filter_keyword'];
		//else
		//	$keyword = $this->Session->read("Recruitment_keyword");
		//
		//$this->Session->write("Recruitment_keyword", $keyword);
		//
		//if($keyword && $keyword != "")
		//{
		//	$conditions["Recruitment.name LIKE ?"] = '%'.$keyword.'%';
		//	//$conditions["Product.name LIKE ?"] = '%'.$keyword.'%';
		//}
		//
		//var_dump($this->params);
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions				
			);
		}
		
		$this->set('Recruitments', $this->paginate());
		$this->set('cities', $cities);
		$this->set('city_id', $city_id);
		$this->set('districts', $districts);		
		$this->set('district_id', $district_id);
		$this->set('careers', $careers);		
		$this->set('career_id', $career_id);
		$this->set('types', $types);		
		$this->set('type_id', $type_id);
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Recruitment', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('recruitment', $this->Recruitment->read(null, $id));
	}

	function admin_add() {
		$city_id = 1;
		$usr = $this->Auth->user();
		if (!empty($this->data)) {
			
			
			
			$this->Recruitment->create();
			if ($this->Recruitment->save($this->data)) {
				$this->Session->setFlash(__('The Recruitment has been saved', true));
				
				
				
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Recruitment could not be saved. Please, try again.', true));
			}
			$city_id = $this->data["Recruitment"]["city_id"];
			$this->data["Recruitment"]["user_id"] = $usr["User"]["id"];
			
			
			
		}
		
		$users = $this->Recruitment->User->find('list');
		$cities = $this->Recruitment->City->find('list');
		$dits = $this->Recruitment->District->find('list', array("conditions"=>array("city_id"=>$city_id)));
		$districts[0] = "- ".__('Quận/Huyện', true)." -";
		foreach($dits as $key => $value)
		{
			$districts[$key] = $value;
		}
		$cas = $this->Recruitment->Career->find('list');
		$careers[0] = "- ".__('Ngành nghề', true)." -";
		foreach($cas as $key => $value)
		{
			$careers[$key] = $value;
		}
		$recruitmentTypes = $this->Recruitment->RecruitmentType->find('list');
		$this->set(compact('users', 'cities', 'districts', 'careers', 'recruitmentTypes'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Recruitment', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			
			
			$pf = $this->Recruitment->read(null, $this->data["Recruitment"]["id"]);
			
			if ($this->Recruitment->save($this->data)) {
				$this->Session->setFlash(__('The Recruitment has been saved', true));
				
					
				
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Recruitment could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Recruitment->read(null, $id);
		}
		$users = $this->Recruitment->User->find('list');
		$cities = $this->Recruitment->City->find('list');
		$districts = $this->Recruitment->District->find('list');
		$careers = $this->Recruitment->Career->find('list');
		$recruitmentTypes = $this->Recruitment->RecruitmentType->find('list');
		$this->set(compact('users', 'cities', 'districts', 'careers', 'recruitmentTypes'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Recruitment', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Recruitment->delete($id)) {
			$this->Session->setFlash(__('Recruitment deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Recruitment was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
